package com.yuexunit.operationman.nbsy.module.nurse.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.ExitApplication;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.module.nurse.Bean.NurseBean;
import com.yuexunit.operationman.nbsy.module.nurse.model.impl.NurseModel;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.INursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.impl.NursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.ui.view.INurseView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.ClickDialog;
import com.yuexunit.operationman.nbsy.widget.CustomDatePicker;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/7 0007.
 */

public class Act_Ops_End extends BaseKJActivity implements ISafeOperationSignView, INurseView {
    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.pre_ops)
    CheckBox preOps;
    @Bind(R.id.skin_injury)
    CheckBox skinInjury;
    @Bind(R.id.skin_other)
    CheckBox skinOther;
    @Bind(R.id.skin_other_txt)
    EditText skinOtherTxt;
    @Bind(R.id.iv_skin_other)
    LinearLayout ivSkinOther;
    @Bind(R.id.ops_pre_checkbox_03_01)
    CheckBox opsPreCheckbox0301;
    @Bind(R.id.ops_pre_checkbox_03_02)
    CheckBox opsPreCheckbox0302;
    @Bind(R.id.ops_pre_checkbox_03_03)
    CheckBox opsPreCheckbox0303;
    @Bind(R.id.after_return_00)
    CheckBox afterReturn00;
    @Bind(R.id.after_return_01)
    CheckBox afterReturn01;
    @Bind(R.id.after_return_02)
    CheckBox afterReturn02;
    @Bind(R.id.after_return_03)
    CheckBox afterReturn03;
    @Bind(R.id.leave_time)
    TextView leaveTime;
    @Bind(R.id.operation_name)
    EditText operationName;
    @Bind(R.id.operation_diagnose)
    EditText operationDiagnose;

    //签名
    @Bind(R.id.img_ra_sign_03)
    ImageView imgRaSign03;
    @Bind(R.id.btn_ra_sign_03)
    TextView btnRaSign03;
    @Bind(R.id.tv_ra_sign_03_time)
    TextView tvRaSign03Time;
    @Bind(R.id.tv_ra_sign_03_name)
    TextView tvRaSign03Name;

    LoadingDialog loadingDialog;
    INursePresenter presenter;

    ISafeOperationSignPresenter presenter_sign;
    LoginDialog loginDialog;

    ClickDialog dialog;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_ops_end);
        ButterKnife.bind(this);
        ExitApplication.getInstance().addActivity(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(this);
        presenter = new NursePresenter(this, new NurseModel());
        presenter_sign = new SafeOperationSignPresenter(this, new SafeOperationSignModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.NURSEBEAN.getObject().getNursing().getPatientName());
        if (AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.NURSEBEAN.getObject().getNursing().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.NURSEBEAN.getObject().getNursing().getOperationName());
    }

    private void setContent() {
        for (NurseBean.ObjectEntity.NursingItemListEntity p : AppConfig.NURSEBEAN.getObject().getNursingItemList()) {
            switch (p.getItemCode()) {
                case "AP_ShouShuMingChen":
                    if (p.getItemValue().length() > 0) {
                        operationName.setText(p.getItemValue());
                    }
                    break;
                case "AP_ShuHouZhenDuan":
                    if (p.getItemValue().length() > 0) {
                        operationDiagnose.setText(p.getItemValue());
                    }
                    break;
                case "AP_YiShiQingKuang":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            opsPreCheckbox0301.setChecked(true);
                        }
                        if (skin[1].equals("1")) {
                            opsPreCheckbox0302.setChecked(true);
                        }
                        if (skin[2].equals("1")) {
                            opsPreCheckbox0303.setChecked(true);
                        }
                    }
                    break;
                case "AP_ShuHouSongHui":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            afterReturn00.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            afterReturn01.setChecked(true);
                        } else if (skin[2].equals("1")) {
                            afterReturn02.setChecked(true);
                        } else if (skin[3].equals("1")) {
                            afterReturn03.setChecked(true);
                        }
                    }
                    break;
                case "AP_LiShiShiJian":
                    if (p.getItemOther().length() > 0) {
                        leaveTime.setText(p.getItemOther());
                    }
                    break;
                case "AP_Skin":
                    if (p.getItemValue().length() > 0) {
                        String[] skin = p.getItemValue().split(";", -1);
                        if (skin[0].equals("1")) {
                            preOps.setChecked(true);
                        } else if (skin[1].equals("1")) {
                            skinInjury.setChecked(true);
                        }
                    }

                    if (p.getItemOther().length() > 0) {
                        skinOther.setChecked(true);
                        skinOtherTxt.setText(p.getItemOther());
                        ivSkinOther.setVisibility(View.VISIBLE);
                    }
                    break;
                case "AP_Over_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign03.setVisibility(View.VISIBLE);
                        btnRaSign03.setVisibility(View.GONE);
                        tvRaSign03Time.setVisibility(View.VISIBLE);
                        tvRaSign03Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign03);
                        tvRaSign03Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign03Name.setText(p.getItemName());
                    }
                    break;
            }
        }
    }

    private void insertContent() {
        for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
            String value = "";
            switch (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode()) {
                case "AP_ShouShuMingChen":
                    if (operationName.getText().toString().length() > 0) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(operationName.getText().toString());
                    }
                    break;
                case "AP_ShuHouZhenDuan":
                    if (operationDiagnose.getText().toString().length() > 0) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(operationDiagnose.getText().toString());
                    }
                    break;
                case "AP_YiShiQingKuang":
                    if (opsPreCheckbox0301.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;;");
                    }
                    if (opsPreCheckbox0302.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1;");
                    }
                    if (opsPreCheckbox0303.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";;1");
                    }
                    break;
                case "AP_ShuHouSongHui":
                    if (afterReturn00.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;;;");
                    }
                    if (afterReturn01.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1;;");
                    }
                    if (afterReturn02.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";;1;");
                    }
                    if (afterReturn03.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";;;1");
                    }
                    break;
                case "AP_LiShiShiJian":
                    AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(leaveTime.getText().toString());
                    break;
                case "AP_Skin":
                    if (preOps.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue("1;");
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther("");
                    } else if (skinInjury.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";1");
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther("");
                    } else if (skinOther.isChecked()) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";");
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(skinOtherTxt.getText().toString());
                    } else {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(";");
                    }
                    break;

            }
        }

        if (AppConfig.NURSEBEAN.getObject().getNursing().getNursingId() == null) {
            Map<String, String> params = new HashMap<>();
            params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
            presenter.insertNurseDate(params);
        } else {
            Map<String, String> params = new HashMap<>();
            params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
            params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
            params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
            presenter.updateNurseDate(params);
        }
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if (signBean.getCode() == 200) {
            if (signBean.getObject().getDoctorType().equals("H")) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                Date curDate = new Date(System.currentTimeMillis());//获取当前时间
                String str = formatter.format(curDate);
                imgRaSign03.setVisibility(View.VISIBLE);
                Glide.with(this)
                        .load(signBean.getObject().getSignPicture())
                        .placeholder(R.color.white)
                        .error(R.color.white)
                        .into(imgRaSign03);
                btnRaSign03.setVisibility(View.GONE);
                tvRaSign03Time.setVisibility(View.VISIBLE);
                tvRaSign03Time.setText("签名时间:" + str);
                tvRaSign03Name.setVisibility(View.VISIBLE);
                tvRaSign03Name.setText(signBean.getObject().getDoctorName());

                for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                    if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("AP_Over_Sign")) {
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                        AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                    }
                }
            } else {
                ViewInject.toast("请使用护士账号进行签名");
            }
        } else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    @Override
    public void insertNurseDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateNurseDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.pre_ops,
            R.id.skin_injury, R.id.skin_other, R.id.ops_pre_checkbox_03_01,
            R.id.ops_pre_checkbox_03_02, R.id.ops_pre_checkbox_03_03,
            R.id.after_return_00, R.id.after_return_01,
            R.id.after_return_02, R.id.after_return_03, R.id.leave_time, R.id.sure
            , R.id.btn_ra_sign_03})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.pre_ops:
                if (skinInjury.isChecked()) {
                    skinInjury.setChecked(false);
                }
                if (skinOther.isChecked()) {
                    skinOther.setChecked(false);
                    ivSkinOther.setVisibility(View.GONE);
                }
                break;
            case R.id.skin_injury:
                if (preOps.isChecked()) {
                    preOps.setChecked(false);
                }
                if (skinOther.isChecked()) {
                    skinOther.setChecked(false);
                    ivSkinOther.setVisibility(View.GONE);
                }
                break;
            case R.id.skin_other:
                if (preOps.isChecked()) {
                    preOps.setChecked(false);
                }
                if (skinInjury.isChecked()) {
                    skinInjury.setChecked(false);
                }
                if (skinOther.isChecked()) {
                    ivSkinOther.setVisibility(View.VISIBLE);
                } else {
                    ivSkinOther.setVisibility(View.GONE);
                }
                break;
            case R.id.ops_pre_checkbox_03_01:
                if (opsPreCheckbox0302.isChecked()) {
                    opsPreCheckbox0302.setChecked(false);
                }
                if (opsPreCheckbox0303.isChecked()) {
                    opsPreCheckbox0303.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_03_02:
                if (opsPreCheckbox0301.isChecked()) {
                    opsPreCheckbox0301.setChecked(false);
                }
                if (opsPreCheckbox0303.isChecked()) {
                    opsPreCheckbox0303.setChecked(false);
                }
                break;
            case R.id.ops_pre_checkbox_03_03:
                if (opsPreCheckbox0301.isChecked()) {
                    opsPreCheckbox0301.setChecked(false);
                }
                if (opsPreCheckbox0302.isChecked()) {
                    opsPreCheckbox0302.setChecked(false);
                }
                break;
            case R.id.after_return_00:
                if (afterReturn01.isChecked()) {
                    afterReturn01.setChecked(false);
                }
                if (afterReturn02.isChecked()) {
                    afterReturn02.setChecked(false);
                }
                if (afterReturn03.isChecked()) {
                    afterReturn03.setChecked(false);
                }
                break;
            case R.id.after_return_01:
                if (afterReturn00.isChecked()) {
                    afterReturn00.setChecked(false);
                }
                if (afterReturn02.isChecked()) {
                    afterReturn02.setChecked(false);
                }
                if (afterReturn03.isChecked()) {
                    afterReturn03.setChecked(false);
                }
                break;
            case R.id.after_return_02:
                if (afterReturn00.isChecked()) {
                    afterReturn00.setChecked(false);
                }
                if (afterReturn01.isChecked()) {
                    afterReturn01.setChecked(false);
                }
                if (afterReturn03.isChecked()) {
                    afterReturn03.setChecked(false);
                }
                break;
            case R.id.after_return_03:
                if (afterReturn00.isChecked()) {
                    afterReturn00.setChecked(false);
                }
                if (afterReturn01.isChecked()) {
                    afterReturn01.setChecked(false);
                }
                if (afterReturn02.isChecked()) {
                    afterReturn02.setChecked(false);
                }
                break;
            case R.id.leave_time:
                initDatePicker(leaveTime);
                break;
            case R.id.sure:
                boolean sign01=false;
                for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                    if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("AP_Over_Sign")) {
                        if(AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemName().length()>0)
                        {
                            sign01=true;
                        }
                    }
                }

                if(sign01) {
                    insertContent();
                }
                else {
                    dialog = new ClickDialog(aty).builder().setTxt("存在未签名,是否提交？");
                    dialog.isCancleBack(true);
                    dialog.setCallBack(new ClickDialog.ICallBack() {
                        @Override
                        public void callback(Bundle bundle) {
                            if(bundle.getInt("data") == 0){
                                insertContent();
                            } else if(bundle.getInt("data") == 1){
                                dialog.dismiss();
                            }

                        }
                    });
                    dialog.show();
                }
                break;
            case R.id.btn_ra_sign_03:
                showTypeDialog();
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    private void initDatePicker(final TextView view) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Date date1 = null;
        try {
            if (view.getText().toString().length() > 0) {
                date1 = sdf.parse(view.getText().toString());
            } else {
                date1 = new Date();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String now = sdf.format(date1);

        CustomDatePicker customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                view.setText(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(true); // 显示时和分
        customDatePicker1.setIsLoop(true); // 循环滚动
        customDatePicker1.show(now);
    }

}
